package Options::OrderSync::API::OrderPlace;

use strict;
use warnings;
use DE_EPAGES::Core::API::Error qw (Error );
use DE_EPAGES::Trigger::API::Trigger qw ( TriggerHook );
use DE_EPAGES::Core::API::Log qw (LogDebug GetLog);
use DE_EPAGES::Object::API::Factory
    qw( LoadObject ExistsObjectByPath LoadObjectByPath Transaction DeleteObject LoadClassByAlias);
use DE_EPAGES::Order::API::LineItemContainer qw(SaveShipping);
use DE_EPAGES::Object::API::Object::Object qw(clone insertChild);
use Data::Dumper;
use Options::Attributes::API::InterfacePlaceWebOrder;
use Options::UserSync::API::CountriesConverter;
use Options::OrderSync::API::ExtractOrderDetails;

sub NewWebOrder {
    my ($hParams)         = @_;
    my $Order             = $hParams->{Order};
    my $LineItemContainer = $Order->get('LineItemContainer');
    my $Customer          = $Order->get('Customer');
    my $orderToSubmitBIT  = {};

    GetLog->error("NewWebOrder!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!11");

    my $orderToSubmit
        = &Options::OrderSync::API::ExtractOrderDetails::getCreditCardDetails(
        $LineItemContainer);
        
#from this point on we will enrich $orderToSubmit with all other necessary details
    $orderToSubmit
        = &Options::OrderSync::API::ExtractOrderDetails::getBillingAddress(
        $Order, $orderToSubmit );
    $orderToSubmit
        = &Options::OrderSync::API::ExtractOrderDetails::getShippingAddress(
        $Order, $orderToSubmit );
    $orderToSubmit
        = &Options::OrderSync::API::ExtractOrderDetails::addLineItems(
        $LineItemContainer, $orderToSubmit );
    $orderToSubmit
        = &Options::OrderSync::API::ExtractOrderDetails::addCountryCode(
        $Order, $orderToSubmit );
    $orderToSubmit
        = &Options::OrderSync::API::ExtractOrderDetails::addShippingPrice(
        $Order, $orderToSubmit );

    $orderToSubmit->{'UserDefined1'}
        = $Customer->get('HowHearAboutUs')->get('Alias');

    $orderToSubmit->{'Currency'}      = $LineItemContainer->get('CurrencyID');
    $orderToSubmit->{'Type'}          = 7;
    $orderToSubmit->{'CustomerType'}  = 7;
    $orderToSubmit->{'OrderDate'}     = '';
    $orderToSubmit->{'OrderNumber'}   = $Order->get('Alias');
    $orderToSubmit->{'SourceKey'}     = 'WEB1BIB';
    $orderToSubmit->{'DeliverDate'}   = '2011-02-10T00:00:00';
    $orderToSubmit->{'Discount'}      = 0;
    $orderToSubmit->{'OrderDate'}     = '2011-01-14T00:00:00';
    $orderToSubmit->{'VoucherAmount'} = 0;
    $orderToSubmit->{'ChequeAmount'}  = 0;
    $orderToSubmit->{'Reprice'}       = 'true';
    $orderToSubmit->{'Modified'}      = 'true';
    $orderToSubmit->{'CashAmount'}    = 0;
    $orderToSubmit->{'CreditAmount'}  = 0;
    $orderToSubmit->{'CardAmount'}    = 0;

    $orderToSubmit->{'AccountBalance'}     = 0;
    $orderToSubmit->{'AccountBalanceCurr'} = 0;

    # Or maybe OptionsShopperID

    my $OptionsCustNumber = $Customer->get('OptionsCustId');
    if (  !$OptionsCustNumber
        || $OptionsCustNumber eq ""
        || !defined($OptionsCustNumber) )
    {
        $OptionsCustNumber = 0;
    }

    $orderToSubmit->{'CustomerNumber'} = $OptionsCustNumber;
    $orderToSubmit->{'ShopperID'}      = $Customer->get('OptionsShopperID');

    my $WSLinkUpdate
        = Options::Attributes::API::InterfacePlaceWebOrder::new(
        "Options::Attributes::API::InterfacePlaceWebOrder",
        "NewWebOrder", "NewWebOrderRequest", $orderToSubmit, "" );
    my $Result = $WSLinkUpdate->hitMe();
    GetLog->debug( "NewWebOrder: " . Dumper($orderToSubmit) );
    GetLog->debug( "NewWebOrder RESPOND: " . Dumper($Result) );
}

1;
